#include<iostream>
using namespace std;
const int MAXN = 105;
const int INF = 32760;
int n,q;
int dis[MAXN],visit[MAXN];
int a[MAXN][MAXN];
int sum = 0;

void Prim(){
    for(int i = 1; i <= n; i++)
	{
        dis[i] = a[1][i];
        visit[i] = 0;
    }
    dis[1] = 0;
    visit[1] = 1;
    int count = 1;
    while(count < n)
	{
        int min = INF;
        int index = 1;
        for(int i = 1; i <= n; i++)
		{
            if(visit[i] == 0 && dis[i] < min)
			{
                min = dis[i];
                index = i;
            }
        }
        visit[index] = 1;
        count++;
        sum+= dis[index];
        for(int i = 1; i <= n; i++)
            if(visit[i] == 0 && dis[i] > a[index][i])
                dis[i] = a[index][i];
    }
}
int main()
{
    cin>>n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin>>a[i][j];
    
    cin>>q; 
    for(int i = 1; i <= q; i++)
	{
        int u, v;
        cin>>u>>v;
        a[u][v] = a[v][u] = 0;
    }
    sum = 0;
    Prim();
    cout<<sum;
    return 0;
}
